<?php

use Illuminate\Support\Facades\Route;

Route::group(['prefix' => 'manage', 'middleware' => ['manage', 'logs:manage'], 'namespace' => 'App\Http\Controllers\Manage'],
    function () {
        // 不登录也可以进入
        Route::group(['middleware' => 'guest:manage'], function () {
            Route::post('login', 'AuthController@login');
            Route::put('refresh', 'AuthController@refresh');
        });
        // 登录才可以进入
        Route::group(['middleware' => 'auth:manage'], function () {
            Route::delete('logout', 'AuthController@logout');
            Route::get('profile/logs', 'AuthController@logs');
            Route::get('profile', 'AuthController@profile');
            Route::put('profile/password', 'AuthController@password');
            Route::post('upload', 'Common\IndexController@upload');
        });
});

Route::group(
    ['prefix' => 'manage/system/dictionary', 'middleware' => ['manage', 'rbac:manage', 'logs:manage'], 'namespace' => 'App\Http\Controllers\Manage\System\Dictionary'],
    function () {
        Route::resource('index', 'IndexController')
            ->name('update', 'dictionary.update')
            ->name('store', 'dictionary.store')
            ->name('index', 'dictionary.index')
            ->name('update', 'dictionary.update')
            ->except(['destroy', 'show', 'create']);
        Route::resource('country', 'CountryController')->name('edit', 'country.update')->except(['destroy', 'show', 'create']);
        Route::resource('currency', 'CurrencyController')->name('edit', 'country.update')->except(['destroy', 'show', 'create']);
        Route::resource('port', 'PortController')->name('edit', 'country.update')->except(['destroy', 'show', 'create']);
        Route::resource('transport', 'TransportController')->name('edit', 'country.update')->except(['destroy', 'show', 'create']);
        Route::resource('unit', 'UnitController')->name('edit', 'country.update')->except(['destroy', 'show', 'create']);
        Route::resource('hs_code', 'HsCodeController')->name('edit', 'country.update')->except(['destroy', 'show', 'create']);
        Route::resource('region', 'RegionController')->name('edit', 'country.update')->except(['destroy', 'show', 'create']);
        Route::resource('positive', 'PositiveController')->except(['destroy', 'update', 'edit', 'store', 'show', 'create']);
    });

Route::group(
    ['prefix' => 'manage/customer', 'middleware' => ['manage', 'rbac:manage', 'logs:manage'], 'namespace' => 'App\Http\Controllers\Manage\Customer'],
    function () {

        Route::put('index/{id}/confirm', 'IndexController@confirm')->name('customer.confirm');
        Route::put('index/{id}/examine', 'IndexController@examine')->name('customer.examine');
        Route::put('index/{id}/reject', 'IndexController@reject')->name('customer.reject');
        Route::put('index/{id}/frozen', 'IndexController@frozen')->name('customer.frozen');
        Route::put('index/{id}/thaw', 'IndexController@thaw')->name('customer.thaw');
        Route::put('index/{id}/password', 'IndexController@password')->name('customer.password');
        Route::post('index/{id}/user', 'IndexController@user')->name('customer.user');


        Route::resource('index', 'IndexController')->except(['create'])
            ->name('store', 'customer.store')
            ->name('edit', 'customer.update')
            ->name('update', 'customer.update')
            ->name('destroy', 'customer.destroy')
            ->name('show', 'customer.show')
            ->name('index', 'customer.index');
    });

Route::group(
    ['prefix' => 'manage/product', 'middleware' => ['manage', 'rbac:manage', 'logs:manage'], 'namespace' => 'App\Http\Controllers\Manage\Product'],
    function () {
        Route::put('index/{id}/reject', 'IndexController@reject');
        Route::put('index/{id}/examine', 'IndexController@examine');
        Route::get('index/{id}/record', 'IndexController@record');
        Route::put('index/{id}/record', 'IndexController@updateRecord');
        Route::put('index/{id}/lock', 'IndexController@lock');
        Route::put('index/{id}/unlock', 'IndexController@unlock');

        Route::resource('index', 'IndexController')
            ->name('show', 'customer.show')
            ->name('index', 'customer.index')
            ->except(['store', 'edit', 'update', 'destroy', 'create']);
    });


Route::group(
    ['prefix' => 'manage/system', 'middleware' => ['manage', 'rbac:manage', 'logs:manage'], 'namespace' => 'App\Http\Controllers\Manage'],
    function () {
        Route::put('express/{id}/status/{status}', 'System\ExpressController@status')->name('express.status');
        Route::resource('express', 'System\ExpressController')->name('edit', 'express.update');
        Route::resource('{id}/express_blacklist', 'System\ExpressBlacklistController');
    });


Route::group(
    ['prefix' => 'manage', 'middleware' => ['manage', 'logs:manage']],
    function () {
        // 客户管理
        Route::group(['middleware' => ['rbac:manage', 'auth:manage'], 'prefix' => 'customer', 'namespace' => 'App\Http\Controllers\Customer'], function () {
            Route::resource('menu', 'Authorities\MenuController', [
                'names' => [
                    'create' => 'customer.menu.store',
                    'edit' => 'customer.menu.update',
                    'index' => 'customer.menu.index',
                    'update' => 'customer.menu.update',
                    'store' => 'customer.menu.store',
                    'destroy' => 'customer.menu.destroy',
                ],
            ])->except(['show']);

            Route::resource('authority', 'Authorities\AuthorityController', [
                'names' => [
                    'create' => 'customer.authority.store',
                    'edit' => 'customer.authority.update',
                    'index' => 'customer.authority.index',
                    'update' => 'customer.authority.update',
                    'store' => 'customer.authority.store',
                    'destroy' => 'customer.authority.destroy',
                ],
            ])->except(['show']);
        });

        // 仓库管理
        Route::group(['middleware' => ['rbac:manage', 'auth:manage'], 'prefix' => 'warehouse', 'namespace' => 'App\Http\Controllers\Warehouse'], function () {
            Route::resource('menu', 'Authorities\MenuController', [
                'names' => [
                    'create' => 'warehouse.menu.store',
                    'edit' => 'warehouse.menu.update',
                    'index' => 'warehouse.menu.index',
                    'update' => 'warehouse.menu.update',
                    'store' => 'warehouse.menu.store',
                    'destroy' => 'warehouse.menu.destroy',
                ],
            ])->except(['show']);
            Route::resource('authority', 'Authorities\AuthorityController', [
                'names' => [
                    'create' => 'warehouse.authority.store',
                    'edit' => 'warehouse.authority.update',
                    'index' => 'warehouse.authority.index',
                    'update' => 'warehouse.authority.update',
                    'store' => 'warehouse.authority.store',
                    'destroy' => 'warehouse.authority.destroy',
                ],
            ])->except(['show']);
        });

        // 分销管理
        Route::group(['middleware' => ['rbac:manage', 'auth:manage'], 'prefix' => 'distribution', 'namespace' => 'App\Http\Controllers\Distribution'], function () {
            Route::resource('menu', 'Authorities\MenuController', [
                'names' => [
                    'create' => 'distribution.menu.store',
                    'edit' => 'distribution.menu.update',
                    'index' => 'distribution.menu.index',
                    'update' => 'distribution.menu.update',
                    'store' => 'distribution.menu.store',
                    'destroy' => 'distribution.menu.destroy',
                ],
            ])->except(['show']);

            Route::resource('authority', 'Authorities\AuthorityController', [
                'names' => [
                    'create' => 'distribution.authority.store',
                    'edit' => 'distribution.authority.update',
                    'index' => 'distribution.authority.index',
                    'update' => 'distribution.authority.update',
                    'store' => 'distribution.authority.store',
                    'destroy' => 'distribution.authority.destroy',
                ],
            ])->except(['show']);
        });
    });

Route::group(
    ['prefix' => 'manage', 'middleware' => ['manage', 'logs:manage'], 'namespace' => 'App\Http\Controllers\Manage'],
    function () {
        // 要验证权限
        Route::group(
            ['middleware' => ['rbac:manage', 'auth:manage'], 'prefix' => 'authorities'],
            function () {
                Route::get('menu/authority', 'Authorities\MenuController@authority')
                    ->name('role.menu_authority');

                Route::resource('role', 'Authorities\RoleController', [
                    'names' => [
                        'create' => 'role.store',
                        'edit' => 'role.update',
                    ],
                ])->except(['show']);

                Route::put('user/{user}/status', 'Authorities\UserController@status')->name('user.update');
                Route::resource('user', 'Authorities\UserController', [
                    'names' => [
                        'create' => 'user.store',
                        'edit' => 'user.update',
                    ],
                ])->except(['show', 'destroy']);

                Route::resource('menu', 'Authorities\MenuController', [
                    'names' => [
                        'create' => 'menu.store',
                        'edit' => 'menu.update',
                    ],
                ])->except(['show']);

                Route::resource('authority', 'Authorities\AuthorityController', [
                    'names' => [
                        'create' => 'authority.store',
                        'edit' => 'authority.update',
                    ],
                ])->except(['show']);
            }
        );
    }
);
